// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: linchuan

#include "coin2/exchange/bybit_futures/api_order/native_rest_async_asio_subscriber.h"

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <thread>

#include "coin2/base/log.h"
#include "coin2/exchange/bybit_futures/symbology/product.h"

namespace coin2::exchange::bybit_futures::api_order {

void BybitFuturesRestAsyncAsioSubscriber::QueryBalance() {
  auto http_context = rest_client()->QueryBalance();
  if (!http_context) {
    return;
  }
  http_context->handler =
      std::bind(&BybitFuturesRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void BybitFuturesRestAsyncAsioSubscriber::QueryPosition(const std::string& native_symbol) {
  RestParams params;
  params.set_symbol(native_symbol);
  auto http_context = rest_client()->QueryPosition(params);
  if (!http_context) {
    return;
  }
  http_context->handler =
      std::bind(&BybitFuturesRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void BybitFuturesRestAsyncAsioSubscriber::QueryOpenOrders(const std::string& native_symbol) {
  RestParams params;
  params.set_symbol(native_symbol);
  auto http_context = rest_client()->QueryOrders(params);
  if (!http_context) {
    return;
  }
  http_context->handler =
      std::bind(&BybitFuturesRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

#if 0
// exchange abandoned "/open-api/order/list", not used any more
void BybitFuturesRestAsyncAsioSubscriber::QueryAllOrders(const std::string& native_symbol) {
  RestParams params;
  params.set_symbol(native_symbol);
  params.set_limit("50");
  auto http_context = rest_client()->QueryOrders(params);
  if (!http_context) {
    return;
  }
  http_context->handler = std::bind(
      &BybitFuturesRestAsyncAsioSubscriber::HandleQuery,
      this,
      std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}
#endif

void BybitFuturesRestAsyncAsioSubscriber::QueryFill(const std::string& native_symbol) {
  RestParams params;
  params.set_symbol(native_symbol);
  params.set_limit("50");
  auto http_context = rest_client()->QueryFills(params);
  if (!http_context) {
    return;
  }
  http_context->handler =
      std::bind(&BybitFuturesRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void BybitFuturesRestAsyncAsioSubscriber::SubscribeAsync() {
  AddPeriodicTask(
      std::bind(&BybitFuturesRestAsyncAsioSubscriber::QueryBalance, this),
      config_.query_account_balance_period_sec(),
      "query_balance");

  const auto symbol_list = native_symbols();

  AddPeriodicTask(
      std::bind(&BybitFuturesRestAsyncAsioSubscriber::QueryPosition, this, std::placeholders::_1),
      config_.query_account_position_period_sec(),
      "query_position",
      symbol_list);

  AddPeriodicTask(
      std::bind(&BybitFuturesRestAsyncAsioSubscriber::QueryOpenOrders, this, std::placeholders::_1),
      config_.query_open_orders_period_sec(),
      "query_open_orders",
      symbol_list);

  AddPeriodicTask(
      std::bind(&BybitFuturesRestAsyncAsioSubscriber::QueryFill, this, std::placeholders::_1),
      config_.query_fill_period_sec(),
      "query_fill",
      symbol_list);
}

}  // namespace coin2::exchange::bybit_futures::api_order
